package com.jt.redis;

import redis.clients.jedis.Jedis;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 分布式id生成及应用分析
 */
public class IdGeneratorDemo01 {
    public static Long getId(){
        //Jedis jedis=new Jedis("192.168.126.128", 6789);
        Jedis jedis=JedisDataSource.getConnection();
        //jedis.auth("123456"); 假如redis设置了访问密码123456
        Long id = jedis.incr("id");
        jedis.close();
        return id;
    }
    public static void main(String[] args) {
        //构建一个最多只有3个线程的线程池
        ExecutorService es=
        Executors.newFixedThreadPool(3);
        for(int i=1;i<=10;i++){
            //从池中取线程执行任务
            es.execute(new Runnable() {//这个任务会存储到阻塞式任务队列中
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()
                            +"->"+getId());
                }
            });
        }
    }
}
